<template>
  <el-dialog
    :title="dialogType ==='dialog_add'? text_add : text_update"
    :close-on-click-modal="isDebug"
    :visible.sync="dialogVisible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm"  label-width="130px" label-position="right">
    <el-form-item label="类型" prop="type">
      <el-input v-model="dataForm.type" placeholder="1财经要闻 2研究报告 3独家解读 4市场动态"></el-input>
    </el-form-item>
    <el-form-item label="标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="标题"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="description">
      <el-input v-model="dataForm.description" placeholder="手机号"></el-input>
    </el-form-item>

    <el-form-item label="前端跳转url" prop="url">
      <el-input v-model="dataForm.url" placeholder="前端跳转url"></el-input>
    </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="dataForm.createTime"
          type="datetime"
          value-format="timestamp"
          placeholder="选择时间">
        </el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">{{text_cancel}}</el-button>
      <el-button type="primary" @click="dialogType ==='dialog_add'?requestAdd():requestUpdate()">{{text_confirm}}</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        dialogVisible: false,
        dialogType: 'dialog_add', // 弹窗类型 1添加 2编辑,
        dataForm: {
          id: 0,
          type: '',
          title: '',
          description: '',
          url: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          type: [{ validator: this.checkText, trigger: 'change' }],
          title: [{ validator: this.checkText, trigger: 'change' }],
          description: [{ validator: this.checkText, trigger: 'change' }],
          url: [{ validator: this.checkText, trigger: 'change' }],
          createTime: [{ validator: this.checkText, trigger: 'change' }],
          updateTime: [{ validator: this.checkText, trigger: 'change' }]
        }
      }
    },
    props: {
      rowData: {
        type: Object,
        default: null
      }
    },
    methods: {
      initDialog (dialogType) {
        this.dialogVisible = true
        this.dialogType = dialogType
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (dialogType === 'dialog_update') {
            this.requestInfo()
          }
        })
      },
      /** 请求信息(表单回显) */
      requestInfo () {
        this.$httpRequest({
          url: `/api/news/info/${this.rowData.id}`,
          method: 'get'
        }).then((data) => {
          this.dataForm.type = data.type
          this.dataForm.title = data.title
          this.dataForm.description = data.description
          this.dataForm.url = data.url
          this.dataForm.createTime = data.createTime
          this.dataForm.updateTime = data.updateTime
        }, error => {
          this.showErrorToast(error.msg)
        })
      },
       /** 请求添加 */
      requestAdd () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$httpRequest({
              url: `/api/news/add`,
              method: 'post',
              data: {
                'id': this.dataForm.id || undefined,
                'type': this.dataForm.type,
                'title': this.dataForm.title,
                'description': this.dataForm.description,
                'url': this.dataForm.url,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              }
            }).then(() => {
              this.dialogVisible = false
              this.showSuccessToast(this.toast_text_add_success, () => {
                this.$emit('refreshDataList')
              })
            }, error => {
              this.showErrorToast(error.msg)
            })
          }
        })
      },
      /** 请求更新 */
      requestUpdate () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$httpRequest({
              url: `/api/news/add`,
              method: 'post',
              data: {
                'id': this.dataForm.id || undefined,
                'type': this.dataForm.type,
                'title': this.dataForm.title,
                'description': this.dataForm.description,
                'url': this.dataForm.url,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              }
            }).then(() => {
              this.dialogVisible = false
              this.showSuccessToast(this.toast_text_add_success, () => {
                this.$emit('refreshDataList')
              })
            }, error => {
              this.showErrorToast(error.msg)
            })
          }
        })
      }
    }
  }
</script>
